﻿using ConsoleApp1.Uitls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.Services
{
    public class MainService
    {
        private PrintService printService;

        private FreeSqlService freeSqlService;

        private DataUploadOutPartailService dataUploadOutPartailService;
        private DataUploadTempService dataUploadTempService;
        private DataUploadRService dataUploadRService;

        public MainService(PrintService printService,FreeSqlService freeSqlService, DataUploadOutPartailService dataUploadOutPartailService,
            DataUploadRService dataUploadRService,DataUploadTempService dataUploadTempService)
        {
            this.printService = printService;
            this.printService.SetTitle(nameof(MainService));
            this.freeSqlService = freeSqlService;
            this.dataUploadOutPartailService = dataUploadOutPartailService;
            this.dataUploadRService = dataUploadRService;
            this.dataUploadTempService = dataUploadTempService;
        }

        public async Task Start()
        {
            try
            {
                printService.Debug("启动成功！");
                printService.Debug("测试数据库连接");
                if (freeSqlService.From.Ado.ExecuteConnectTest())
                {
                    printService.Info("数据库连接成功！");
                }
                else
                {
                    throw new Exception("数据库连接失败!");
                }
                printService.Info("泰岳服务器连接地址为 http://192.168.1.11:8581/api/v1");
                dataUploadOutPartailService.Init();
                dataUploadRService.Init();
                dataUploadTempService.Init();
                while (true)
                {
                    printService.Info("休眠1分钟");
                    await Task.Delay(1*60*1000);
                    dataUploadOutPartailService.LoopSend();
                    dataUploadTempService.LoopSend();
                    dataUploadRService.LoopSend();
                }
            }
            catch (Exception ex)
            {
                printService.Error("程序运行出现问题");
                printService.Error(ex.ToString());
            }
            
            

        }
    }
}
